package Controllers;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import Controllers.GenericToolsForController.WorkerRolls;
import Entity.Message;
import Entity.Worker;

public class WorkerManager 
{
	
	
	
	/*
	 * Singleton 
	 */
	private static  WorkerManager Instance = null;
	private WorkerManager(){}
	public static WorkerManager getInstance()
	{
		if(Instance == null)
		{
			Instance = new WorkerManager();
		}
		return Instance;
	}
	
	
	public Worker getWorkerEmail(int workerNumber) 
	{
		Message msg = new Message(1001, GlobalData.MESSAGE_ID(), new Integer(workerNumber));
        
		Message AnswerMessage = null;
		
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (Worker) AnswerMessage.getDataGram();

	}
	
	public  List<Worker> getAllWorkers() 
	{
		Message msg = new Message(1002, GlobalData.MESSAGE_ID(), null);
		
		Message AnswerMessage = null;
		
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ( List<Worker>) AnswerMessage.getDataGram();
	
	}
	
	public List<Worker> getIEWorkers() 
	{
		Message msg = new Message(1003, GlobalData.MESSAGE_ID(), null);
		
		Message AnswerMessage = null;
		
		try 
		{
			System.out.println("before send");
			GlobalData.client.sendToServer(msg);
			System.out.println("after send");
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			while (true)
			{
				//System.out.println("while true");
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return  (List<Worker>) AnswerMessage.getDataGram();
	
	}
	
	public Worker getICM_MANGER() 
	{
		Message msg = new Message(1004, GlobalData.MESSAGE_ID(), null);
		
		Message AnswerMessage = null;
		
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (Worker) AnswerMessage.getDataGram();
	
	}
	
	public  List<Worker> getADMINISTATIONWorkers() 
	{
		Message msg = new Message(1005, GlobalData.MESSAGE_ID(), null);
		
		Message AnswerMessage = null;
		
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ( List<Worker>) AnswerMessage.getDataGram();

	}
	public Worker getWorker(int workerNumber) 
	{
		Message msg = new Message(1006, GlobalData.MESSAGE_ID(), new Integer(workerNumber));
		
		Message AnswerMessage = null;
		
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (Worker) AnswerMessage.getDataGram();


	}
	
	public boolean addNewWorker(String userid , String string)
	{
		String[] workerdetails = {userid,string};
		
		Message msg = new Message(1007, GlobalData.MESSAGE_ID(), workerdetails);
		
		Message AnswerMessage = null;
		try 
		{
			GlobalData.client.sendToServer(msg);
			while (true)
			{
				if (GlobalData.client.isRecivedMessage()==true)
				{
					AnswerMessage = GlobalData.client.getAnsweredMessage(msg);
				    if (AnswerMessage!=null)
				  	      break;
				}	
			}
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return (Boolean)AnswerMessage.getDataGram();
		
	
		
	}
	
	
}
